package com.glory.hikvisionmobile.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import com.glory.hikvisionmobile.bean.EqpWork.EqpLotBean;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hx
 * @date 2019/11/15
 * @desc
 */
public class MyUtils {

    public interface  EditTextActionListener{
        void onKeyDown(String s);
    }

    public static List<FreeUI_TabIndexGridEntity> setPicToDrawable(Context context,List<FreeUI_TabIndexGridEntity> entity) {
        for (FreeUI_TabIndexGridEntity og : entity) {
            if (og.getPicName().length() != 0) {
                Drawable drawable = context.getResources()
                        .getDrawable(context.getResources()
                                .getIdentifier(og.getPicName(), "drawable", context.getPackageName()));
                og.setDrawable(drawable);
            }
        }
        return entity;
    }

    public static void editActionListener(EditText editText,EditTextActionListener listener){
        editText.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            //当actionId == XX_SEND 或者 XX_DONE时都触发
            //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
            if (actionId == EditorInfo.IME_ACTION_SEND
                    || actionId == EditorInfo.IME_ACTION_NEXT
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || (keyEvent != null && KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode() && KeyEvent.ACTION_DOWN == keyEvent.getAction())) {
                //处理事件
                listener.onKeyDown(editText.getText().toString().trim());
            }
            return false;
        });
    }

    public static List<EqpLotBean> sortList(List<EqpLotBean> list, int i){
        List<EqpLotBean> collect = new ArrayList<>();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            Comparator<EqpLotBean> byType = null;
            switch (i){
                case 0:
                    byType = Comparator.comparing(EqpLotBean::getLotId);
                    break;
                case 1:
                    byType = Comparator.comparing(EqpLotBean::getMainQty);
                    break;
                case 2:
                    byType = Comparator.comparing(EqpLotBean::getPriority);
                    break;
                case 3:
                    byType = Comparator.comparing(EqpLotBean::getRqTime);
                    break;
                case 4:
                    byType = Comparator.comparing(EqpLotBean::getRecipeName);
                    break;
                case 5:
                    byType = Comparator.comparing(EqpLotBean::getStopTime).reversed();
                    break;
                case 6:
                    byType = Comparator.comparing(EqpLotBean::getPartDesc);
                    break;
            }
            collect = list.stream().sorted(byType).collect(Collectors.toList());
        }

        return collect;
    }
}
